package com.gkwave.action.service;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gkwave.module.bean.Login;
import com.gkwave.module.bean.User;
import com.gkwave.module.dao.UserDAO;
import com.gkwave.util.CloseTradingProduct;

/**
 * @author Dhanasekar Rengasamy
 * @version 1.0.0
 * Servlet implementation class LoginServlet
 */
public class LoginService extends BaseService {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginService() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * Proceses requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    public void procesRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String username = request.getParameter("username");
    	String password = request.getParameter("password");
    	
    	out = response.getWriter();
    	String status = null;
    	String message = null;
    	String nmFullname = null;
    	Integer idUser = 9999;
    	if (username != null && password != null) {
	    	String query = "SELECT id_user,nm_fullname FROM M_USER WHERE ID_LOGIN='"+username+"' and NM_PASSWORD='"+password+"'";
	    	UserDAO userDAO = new UserDAO();
	    	User user = null;
			try {
				user = userDAO.findUser(query);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (user != null) { 
					status = "SUCCESS";
					message = "Login Successful";
					idUser = user.getIdUser();
					nmFullname = user.getFirstName();
					new CloseTradingProduct();
			} else {
	    		status = "ERROR";
	    	    message = "Login Failed";
	    	}
    	} else {
    		status = "ERROR";
    	    message = "Login Failed";
    	}
    	Login login = new Login(idUser,nmFullname,status , message);
    	xmlResponse = login.marsel(login);
    	System.out.println(xmlResponse);
    	out.println(xmlResponse);
    }

}
